package com.zunipe.accounts.ui;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zunipe.accounts.R;
import com.zunipe.accounts.database.entity.Account;
import com.zunipe.accounts.database.handler.AccountHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Map<String, List<Account>> list;
    private AccountHandler accountHandler;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private String date;
    private Map<Integer, String> headerMap;
    private int lastPosition;
    private Context context;

    public AccountAdapter(String date) {
        this.date = date;
        accountHandler = new AccountHandler();
    }

    public void update(String date) {
        this.date = date;
        list = accountHandler.getList(date);
        headerMap = new HashMap<>();
        int cnt = 0;
        lastPosition = 0;
        for (String key : list.keySet()) {
            int count = list.get(key).size();
            if (count > 0) {
                headerMap.put(cnt, key);
                cnt += (1 + count);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (headerMap.containsKey(position))
            return TYPE_HEADER;
        else
            return TYPE_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_account_header_layout, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_account_item_layout, parent, false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (headerMap.containsKey(position)) {
            ((HeaderViewHolder) holder).bind(headerMap.get(position));
            lastPosition = holder.getAdapterPosition();
        } else {
            Account account = list.get(headerMap.get(lastPosition)).get(position - lastPosition - 1);
            ((ItemViewHolder) holder).bind(account);

            ((ItemViewHolder) holder).root.setOnLongClickListener(v -> {
                PopupMenu popup = new PopupMenu(context, v);
                popup.getMenuInflater().inflate(R.menu.popup_long_click_menu, popup.getMenu());
                popup.setOnMenuItemClickListener(item -> {
                    int id = item.getItemId();
                    if (id == R.id.delete) {
                        accountHandler.deleteById(account.getId());
                        Intent intent = new Intent();
                        intent.setAction("com.zunipe.accounts.update.list");
                        intent.setPackage("com.zunipe.accounts");
                        context.sendBroadcast(intent);
                    } else if (id == R.id.edit) {
                        Intent intent = new Intent();
                        intent.setAction("com.zunipe.accounts.open.edit.dialog");
                        intent.setPackage("com.zunipe.accounts");
                        intent.putExtra("id", account.getId());
                        intent.putExtra("type", account.getAmount() > 0 ? "income" : "expenses");
                        context.sendBroadcast(intent);
                    }
                    return true;
                });

                popup.show();
                return true;
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = 0;
        for (String key : list.keySet()) {
            count += (1 + list.get(key).size());
        }
        return count;
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView headerText;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            headerText = itemView.findViewById(R.id.tv_header);
        }

        public void bind(String date) {
            headerText.setText(date);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        View root;
        ImageView ivType;
        TextView tvInfo;
        TextView tvAmount;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView;
            ivType = itemView.findViewById(R.id.iv_type);
            tvInfo = itemView.findViewById(R.id.tv_info);
            tvAmount = itemView.findViewById(R.id.tv_amount);
        }

        public void bind(Account account) {
            if (account.getNotes() != null) {
                tvInfo.setText(account.getNotes());
            } else {
                tvInfo.setText(account.getType());
            }
            setIcon(account.getType());
            tvAmount.setText(String.valueOf(account.getAmount()));
        }

        public void setIcon(String type) {
            switch (type) {
                case "餐饮":
                    ivType.setImageResource(R.drawable.icon5);
                    break;
                case "购物":
                    ivType.setImageResource(R.drawable.icon6);
                    break;
                case "日用":
                    ivType.setImageResource(R.drawable.icon8);
                    break;
                case "交通":
                    ivType.setImageResource(R.drawable.icon3);
                    break;
                case "蔬菜":
                    ivType.setImageResource(R.drawable.icon7);
                    break;
                case "水果":
                    ivType.setImageResource(R.drawable.icon4);
                    break;
                case "零食":
                    ivType.setImageResource(R.drawable.icon9);
                    break;
                case "娱乐":
                    ivType.setImageResource(R.drawable.icon10);
                    break;
                case "工资":
                    ivType.setImageResource(R.drawable.icon11);
                    break;
                case "兼职":
                    ivType.setImageResource(R.drawable.icon12);
                    break;
                case "理财":
                    ivType.setImageResource(R.drawable.icon13);
                    break;
                case "礼金":
                    ivType.setImageResource(R.drawable.icon1);
                    break;
                case "其他":
                    ivType.setImageResource(R.drawable.icon14);
                    break;
            }
        }
    }
}
